package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.exception.NoSuchAxisException;
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.task.DataSaveUtil;
import com.guiji.quartz.util.Enums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-10-19 17:17
 **/
public class Shenzhen {
	private static final Logger log = LoggerFactory.getLogger(Shenzhen.class);

	public void mainMethod() throws NoSuchFunctionException, XpathSyntaxErrorException, NoSuchAxisException {
		String preUrl,firstUrl;
		for (Enums.newspapers newspaper : Enums.newspapers.values()) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String[] ss = sdf.format(new Date()).split("-");
			String year = ss[0];
			String mon = ss[1];
			String day = ss[2];
			preUrl=newspaper.getKey();
			//http://sztqb.sznews.com/PC/layout/202110/19/node_A01.html
			firstUrl = preUrl+"PC/layout/"+year+mon+"/"+day+"/node_A01.html";
			HttpResponse response ;
			try {
				response = HttpRequest.get(firstUrl).
					header("accept-encoding", "gzip, deflate, br")
					.header("Cookie", "HMF_CI=0f6ce03ea60c5d8ff902a5ac8be105650af28965ad25f7aabf17d4424e0dac85d9")
					.setConnectionTimeout(15000)
					.execute();
			} catch (Exception e) {
				continue;
			}
			String resp = response.body();
			//获取子链接
			Set<String> listContent = getBankuaiUrl(resp, preUrl);
			for (String bankuaiUrl:listContent) {
				//执行板块爬虫
				doBankuai(bankuaiUrl,preUrl,newspaper.getValue());
			}
		}
	}

	private void doBankuai(String bankuaiUrl,String preUrl,String category) throws NoSuchFunctionException, XpathSyntaxErrorException, NoSuchAxisException {
		HttpResponse response = null;
		try {
			response = HttpRequest.get(bankuaiUrl).
				header("accept-encoding", "gzip, deflate, br")
				.header("Cookie", "HMF_CI=0f6ce03ea60c5d8ff902a5ac8be105650af28965ad25f7aabf17d4424e0dac85d9")
				.setConnectionTimeout(15000)
				.execute();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		String resp = response.body();
		Set<String> contentUrl = getContentUrl(resp,preUrl);
		getContent(contentUrl,category,preUrl);

	}

	private Set<String> getBankuaiUrl(String resp, String url) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String[] ss = sdf.format(new Date()).split("-");
		String year = ss[0];
		String mon = ss[1];
		String day = ss[2];

		Set<String> res = new HashSet<>();
		String pattern = "node_.*?.html";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(resp);
		while (m.find()) {
			res.add(url+"PC/layout/"+year+mon+"/"+day +"/"+ m.group());
		}
		return res;
	}

	private static Set<String> getContentUrl(String content,String url) {
		Set<String> res = new HashSet<>();
		String pattern = "../../../content/.*?.html";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(content);
		while (m.find()) {
			res.add(m.group().replaceAll("../../../",url+"PC/"));
		}
		return res;
	}

	private void getContent(Set<String> listContent,String category,String preUrl) throws NoSuchFunctionException, XpathSyntaxErrorException, NoSuchAxisException {
		for (String url : listContent) {
			log.info("getContent执行到-{},listContent长度-{}", url, listContent.size());
			String doc;
			JXDocument jxDocument = null;

			HttpResponse response = HttpRequest.get(url).setConnectionTimeout(15000).execute();
			doc = response.body();
			jxDocument = new JXDocument(doc);

			String title;
			try {
				title = (String) jxDocument.sel("//h3/text()").get(0);
			} catch (Exception e) {
				log.error("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}

			StringBuffer content = new StringBuffer();
			jxDocument.sel("//founder-content//p/text()").forEach(s->{
				content.append(s+"$$$");
			});

			List<Object> imgs = null;
			try {
				imgs = jxDocument.sel("//div[@class='newsdetatext']//img/@src");
			} catch (Exception e) {
				log.error("获取图片失败");
			}
			List<Object> newImgs = dealpic(imgs,preUrl);

			Date date = new Date();

			if (content.length() > 5000 || content.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length() < 20 || newImgs.size() > 20) {
				continue;
			}
			try {
				DataSaveUtil.saveData(title, content.toString(), newImgs, url, date, "http://sztqb.sznews.com/", "深圳报业-" +category);
				log.info(Thread.currentThread().getName() + "-----------------深圳报业-{}----------------", category);
			} catch (Exception e) {
				continue;
			}
		}
	}

	private List<Object> dealpic(List<Object> imgs,String preUrl) {
		List<Object> list = new ArrayList<>();
		for(Object img:imgs){
			list.add(img.toString().replaceAll("../../../../",preUrl));
		}
		return list;
	}

}
